package api

import (
	"errors"
	"singo/service"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

// OrganizationCreate 新增组织
// @Summary 新增组织
// @Router /api/v1/organization/create [post]
// @tags 组织相关
// @accept application/json
// @Param name query string true "组织名称"
// @Param intro query string false "介绍"
// @Param avatar query string false "头像"
// @Param score query int false "组织得分"
// @Param created_by query string true "创建人"
// @Param parent_id query string false "组织 - 父子"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func OrganizationCreate(c *gin.Context) {
	var service service.OrganizationCreateService
	session := sessions.Default(c)
	userUID := session.Get("user_uid")
	if userUID == nil {
		c.JSON(200, ErrorResponse(errors.New("不存在此用户信息")))
		return
	}
	if err := c.ShouldBind(&service); err == nil {
		res := service.OrganizationCreate(userUID.(string))
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// OrganizationList 组织列表接口
// @Summary 组织列表
// @Router /api/v1/organizations [get]
// @tags 组织相关
// @accept application/json
// @Param user_id query string false "用户ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func OrganizationList(c *gin.Context) {
	service := service.ListOrganizationService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.OrganizationList()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// OrganizationShow 组织详情接口
// @Summary 组织详情
// @Router /api/v1/organization/{id} [get]
// @tags 组织相关
// @accept application/json
// @Param id path string false "组织ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func OrganizationShow(c *gin.Context) {
	service := service.OrganizationShowService{}
	res := service.OrganizationShow(c.Param("id"))
	c.JSON(200, res)
}

// OrganizationUpdate 更新组织接口
// @Summary 更新组织
// @Router /api/v1/organization/{id} [put]
// @tags 组织相关
// @accept application/json
// @Param id path string false "组织ID"
// @Param name query string false "组织名称"
// @Param intro query string false "组织介绍"
// @Param avatar query string false "组织头像"
// @Param score query int false "组织得分"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func OrganizationUpdate(c *gin.Context) {
	service := service.OrganizationUpdateService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.OrganizationUpdate()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// OrganizationDelete 删除组织接口
// @Summary 删除组织
// @Router /api/v1/organization/{id} [delete]
// @tags 组织相关
// @accept application/json
// @Param id path string false "组织ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func OrganizationDelete(c *gin.Context) {
	service := service.OrganizationDeleteService{}
	res := service.OrganizationDelete(c.Param("id"))
	c.JSON(200, res)
}
